Ext.define('PJ.view.setassessmentpaper.PreSelectStaffList', {
    extend: 'AM.default.DefaultList',
    
    alias: ['widget.preSelectStaffList'],
    
    title: '待选列表',

    queryUnitId: null,
    searchTreePath: null,
    overflowX: 'auto',
    overflowY: 'hidden',
    columns: [{
        header: '问卷名称',
        dataIndex: 'questionnaireName',
        flex: 1,
        minWidth: 120,
		renderer: function(value, metaData, record){
			metaData.tdAttr = 'data-qtip="' + value + '"';
			return value;
		}
    },{
        header: '问卷说明',
        dataIndex: 'questionnaireExplain',
        flex: 1,
        minWidth: 120,
        renderer: function(value, metaData, record){
            return value;
        }
    },{
        header: '创建时间',
        dataIndex: 'q.createDate',
        width: 100,
        renderer: function(value, metaData, record){
            return value;
        }
    }],
    
    initComponent: function(){
        this.store = Ext.create('PJ.store.setassessmentpaper.PreSelectStaffList');
        this.loadType = gj_onlineLearning_constant.loadType;//加载方式
        this.callParent(arguments);
    },

    onAddClick: function(button){
        var me = this;
        var records = this.getSelectionModel().getSelection();
        
        var userIds = new Array();
        Ext.Array.each(records, function(name, index){
        	userIds.push(name.get("questionnaireId"));
        });
        if(records.length>1){
        	Orchid.alert("提示","每次只能添加一套试卷！");
        	return;
        }
        var chooseSelectStaffList = me.up("window").down("chooseSelectStaffList");
        chooseSelectStaffList.getSelectionModel().selectAll();
        var recs = chooseSelectStaffList.getSelectionModel().getSelection();
        if(recs != null && recs.length >= 1){
        	Orchid.alert("提示","已添加过试卷，请先删除后再进行添加！");
        	return;
        }else{
//        	me.loadMask.show();
            if(gj_onlineLearning_constant.loadType == "jsonp"){
            	var store = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
    				loadType: gj_onlineLearning_constant.loadType,
    				url: me.store.addUrl,
    				extraParams: {
    					questionnaireId: records[0].get("questionnaireId"),
    					surveyObjectId: me.up("window").classId,
    					surveyObjectIds: me.up("window").surveyObjectIds
    					//questionnaireId: me.up("window").trainingRoleId,
                        //"springmvc.TOKEN": token
    				}
    			});
    			store.load(function(rec, operation, success){
//    				me.loadMask.hide();
    				if(success){
    					Orchid.alert('消息', '添加成功！', function(){
                            var store = me.getStore();
                            store.load();
                            
                            // 刷新已选面板
                            me.up("window").down("chooseSelectStaffList").getStore().load();
                            me.up("window").grid.getStore().load();
                        });
    				}else{
    					Orchid.alert('消息', operation.error);
    				}
    			});
    			/*
            	var store1 = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
    				loadType: gj_onlineLearning_constant.loadType,
    				url: gj_onlineLearning_constant.tokenUrl,
    				extraParams: {}
    			});
    			store1.load(function(rec, operation, success){
    				var token = operation.response;
    				if(success){
    					
    				}else{
    					Orchid.alert('消息', operation.error);
    				}
    			});
    			*/
            }else if(gj_onlineLearning_constant.loadType == "ajax"){
            	Ext.Msg.confirm({
                    title: '提示',
                    msg: '您确定要添加这些信息吗?',
                    buttons: Ext.Msg.OKCANCEL,
                    fn: function(id){
                        if (id == 'ok') {
//                            me.loadMask.show();
                            Ext.Ajax.request({
                                url: me.store.addUrl,
                                params: {
                                	questionnaireId: records[0].get("questionnaireId"),
                                	surveyObjectId: me.up("window").classId,
                                	surveyObjectIds: me.up("window").surveyObjectIds
                                    //"springmvc.TOKEN": token
                                },
                                success: function(response){
//                                    me.loadMask.hide();
                                    var data = response.responseText;
                                    var json = Ext.decode(data, true);
                                    if (json.success) {
//                                        Ext.Msg.alert('消息', json.msg, function(){
                                    	Orchid.alert('消息', '添加成功！', function(){
                                            var store = me.getStore();
                                            store.load();
                                            
                                            // 刷新已选面板
                                            me.up("window").down("chooseSelectStaffList").getStore().load();
                                            me.up("window").grid.getStore().load();
                                        });
                                    }
                                    else {
                                        Ext.Msg.alert('消息', json.msg, function(){
                                            var store = me.getStore();
                                            store.load();
                                            me.up("window").grid.getStore().load();
                                        });
                                    }
                                }
                            });
                        }
                        else {
                        	Ext.getCmp("loadingMask").hide();
                            return;
                        }
                    }
                });
            	/*
            	Ext.Ajax.request({
                    url: gj_onlineLearning_constant.tokenUrl,
                    success: function(response){
                        Ext.getCmp("loadingMask").hide();
                        var data = response.responseText;
                        var token = Ext.decode(data, true);
                        
                    }
                });
                */
            }
        }
    },
    listeners: {
        selectionchange: function(selModel, selected){
            this.down('#' + this.oldId + "Add").setDisabled(selected.length === 0);
        },
        viewready: function(){
        	var me = this;
    		this.addDocked([/*{
                dock: 'top',
                xtype: "searchform",
                columnCount: 1,
                fieldDefaults: {
                    labelAlign: 'right',
                    labelWidth: 100
                },
                items: [{
    				xtype: "textfield",
    				fieldLabel: '问卷名称',
    				name: "searchQuestionnaireName",
    				maxLength: "50",
    				emptyText: "请输入问卷名称"
    			}],
                grid: this,
                onResetClick: function(button){
                    var form = button.up('form');
                    var baseForm = form.getForm();
                    var gridPanel = form.grid;
                    var newParams = {
                    		searchType: "Nonselected",
                    		searchTrainingRoleId: gridPanel.up("window").trainingRoleId,
                    		searchClassId: gridPanel.up("window").classId,
                    		searchDepId: me.queryUnitId
                    };
                    gridPanel.getStore().proxy.extraParams = {};
                    
                    var params = gridPanel.getStore().proxy.extraParams;
                    Ext.apply(params, newParams);
                    
                    form.getForm().reset();
                }
            },*/{
    			xtype: 'toolbar',
    			dock: 'top',
    			displayInfo: true,
    			items: [{
    	        	text: '添加',
    	            iconCls: 'addIco',
    	            itemId: this.oldId + "Add",
    	            scope: this,
    	            disabled: true,
    	            handler: function(button){
    	                button.up("grid").onAddClick(button);
    	            }
    	        }, "->", "->",this.countSelectPanel]
    		}]);
    		// 触发权限过滤事件
    		this.fireEvent("permission", this);
    		var store = me.getStore();
    		var frame = me.up("selectStaffRightFrame");
    		if(!frame){
    			frame = me.up("selectStaffRightCourseFrame");
    		}
            var newParams = {
            		searchType: "Nonselected",
            		surveyObjectId: frame.classId
            };
            var params = me.getStore().proxy.extraParams;
            Ext.apply(params, newParams);
            store.isLoad = true;
            store.load();
    	},
    	afterrender: function(){
            this.loadMask = new Ext.LoadMask(this, {
                msg: "正在加载中,请稍后...."
            });
        }
    }
});
